package com.ai.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class AIMap {

	public static String provider;
	public static LocationManager locationManager;

	public static LatLng getCurrentLocation(Activity activity) {

		// boolean enabledGPS = service
		// .isProviderEnabled(LocationManager.GPS_PROVIDER);

		locationManager = (LocationManager) activity
				.getSystemService(Context.LOCATION_SERVICE);
		// Define the criteria how to select the locatioin provider -> use
		// default

		Criteria criteria = new Criteria();
		provider = locationManager.getBestProvider(criteria, false);
		Location location = locationManager.getLastKnownLocation(provider);
		if (location == null) {
			return null;
		}
		return new LatLng(location.getLatitude(), location.getLongitude());
	}

	public static void configuraPosicao(GoogleMap map, LatLng latLng, int zoom) {
		map.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));
		map.animateCamera(CameraUpdateFactory.zoomTo(zoom), 2000, null);

		CameraPosition cameraPosition = new CameraPosition.Builder()
				.target(latLng).zoom(zoom).bearing(0).tilt(45).build();
		map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
	}

	public static void configuraPosicao(GoogleMap map, LatLng latLng) {
		configuraPosicao(map, latLng, 17);
	}

	public static Marker addMarker(GoogleMap map, LatLng latLng, int icon,
			String title, String snippet) {
		return map.addMarker(new MarkerOptions().position(latLng).title(title)
				.snippet(snippet)
				.icon(BitmapDescriptorFactory.fromResource(icon)));
	}

	public static byte[] readBytes(InputStream inputStream) throws IOException {
		// this dynamically extends to take the bytes you read
		ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();

		// this is storage overwritten on each iteration with bytes
		int bufferSize = 1024;
		byte[] buffer = new byte[bufferSize];

		// we need to know how may bytes were read to write them to the
		// byteBuffer
		int len = 0;
		while ((len = inputStream.read(buffer)) != -1) {
			byteBuffer.write(buffer, 0, len);
		}

		// and then we can return your byte array.
		return byteBuffer.toByteArray();
	}
}
